home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / TW / TW_IBMPC.C next >
C/C++ Source or Header  |  1991-11-04  |  7KB  |  278 lines

  1. /****************************************************************
  2.  
  3.     tw_ibmpc.c      Graphics Text Window
  4.             for IBM PC (tm) and compatibles
  5.             utilizing Microsoft QuickC (tm)
  6.  
  7.             Copyright (c) 1991, Ted A. Campbell
  8.  
  9.             Bywater Software
  10.             P. O. Box 4023
  11.             Duke Station
  12.             Durham, NC  27706
  13.  
  14.             email: tcamp@teer3.acpub.duke.edu
  15.  
  16.     Copyright and Permissions Information:
  17.  
  18.     All U.S. and international copyrights are claimed by the
  19.     author. The author grants permission to use this code
  20.     and software based on it under the following conditions:
  21.     (a) in general, the code and software based upon it may be
  22.     used by individuals and by non-profit organizations; (b) it
  23.     may also be utilized by governmental agencies in any country,
  24.     with the exception of military agencies; (c) the code and/or
  25.     software based upon it may not be sold for a profit without
  26.     an explicit and specific permission from the author, except
  27.     that a minimal fee may be charged for media on which it is
  28.     copied, and for copying and handling; (d) the code must be
  29.     distributed in the form in which it has been released by the
  30.     author; and (e) the code and software based upon it may not
  31.     be used for illegal activities.
  32.  
  33. ****************************************************************/
  34.  
  35. #include "stdio.h"
  36.  
  37. #include "graph.h"
  38.  
  39. #ifdef __STDC__
  40. #include "malloc.h"
  41. #else
  42. extern char * malloc();
  43. #endif
  44.  
  45. #include "bw.h"
  46. #include "gr.h"
  47. #include "tw.h"
  48.  
  49. #define DEF_LINES       25
  50. #define DEF_COLUMNS     80
  51.  
  52. static struct tw_struct * cur_twstruct = NULL;
  53.  
  54. struct tw_struct *
  55. tw_init( rq_x1, rq_y2, rq_lines, rq_cols, min_x1, min_y1, max_x2, max_y2 )
  56.    int rq_x1;                   /* requested x1 (left, graphics) position */
  57.    int rq_y2;                   /* requested y2 (top,  graphics) position */
  58.    int rq_lines;                /* requested text lines */
  59.    int rq_cols;                 /* requested text columns */
  60.    int min_x1;                  /* leftmost   allowable position */
  61.    int min_y1;                  /* bottommost allowable position */
  62.    int max_x2;                  /* rightmost  allowable position */
  63.    int max_y2;                  /* topmost    allowable position */
  64.    {
  65.    static struct videoconfig ibm_vc;
  66.    struct tw_struct *tw;
  67.    int x, y;
  68.  
  69.    /* get memory for tw_struct structure */
  70.  
  71.    if ( ( tw = (struct tw_struct *) malloc( sizeof( struct tw_struct ))) == NULL )
  72.       {
  73.       bw_error( "Failed to find memory for window." );
  74.       return NULL;
  75.       }
  76.  
  77.    /* determine current size of screen and then text fonts */
  78.  
  79.    _getvideoconfig( &ibm_vc );
  80.    tw->fxsize = ibm_vc.numxpixels / DEF_COLUMNS;
  81.    if ( ibm_vc.mode == _HERCMONO )
  82.       {
  83.       tw->fysize = ( ibm_vc.numypixels + 2 ) / DEF_LINES;
  84.       }
  85.    else
  86.       {
  87.       tw->fysize = ibm_vc.numypixels / DEF_LINES;
  88.       }
  89.  
  90. #ifdef OLD_DEBUG
  91.    sprintf( bw_ebuf, "fxsize: %d, fysize: %d",
  92.       tw->fxsize, tw->fysize );
  93.    bw_debug( bw_ebuf );
  94. #endif
  95.  
  96.    /* calculate optimum x1 (left) position for window */
  97.  
  98.    if ( abs( rq_x1 -   ( abs( rq_x1 / tw->fxsize ) * tw->fxsize ) ) >
  99.     abs( rq_x1 - ( ( abs( rq_x1 / tw->fxsize ) * tw->fxsize ) + tw->fxsize )) )
  100.     {
  101.     tw->x1 = ( ( abs ( rq_x1 / tw->fxsize ) * tw->fxsize ) + tw->fxsize );
  102.     }
  103.    else
  104.     {
  105.     tw->x1 = abs( rq_x1 / tw->fxsize ) * tw->fxsize;
  106.     }
  107.  
  108.    /* be sure it is in bounds */
  109.  
  110.    if ( tw->x1 < min_x1 )
  111.       {
  112.       tw->x1 += tw->fxsize;
  113.       }
  114.  
  115.    /* calculate optimum y (top) position for window */
  116.  
  117.    if ( abs( rq_y2 -   ( abs( rq_y2 / tw->fysize ) * tw->fysize ) ) >
  118.     abs( rq_y2 - ( ( abs( rq_y2 / tw->fysize ) * tw->fysize ) + tw->fysize )) )
  119.     {
  120.     tw->y2 = ( ( abs ( rq_y2 / tw->fysize ) * tw->fysize ) + tw->fysize );
  121.     }
  122.    else
  123.     {
  124.     tw->y2 = ( abs ( rq_y2 / tw->fysize ) * tw->fysize );
  125.     }
  126.  
  127.    /* be sure it is in bounds */
  128.  
  129.    if ( tw->y2 > max_y2 )
  130.       {
  131.       tw->y2 -= tw->fxsize;
  132.       }
  133.  
  134. #ifdef OLD_DEBUG
  135.    sprintf( bw_ebuf, "x position: %d, y position: %d",
  136.       tw->x1, tw->y2 );
  137.    bw_debug( bw_ebuf );
  138. #endif
  139.  
  140.    /* calculate requested right screen position */
  141.  
  142.    tw->x2 = tw->x1 + ( rq_cols * tw->fxsize );
  143.  
  144.    /* back up to an acceptable position */
  145.  
  146.    while ( tw->x2 > max_x2 )
  147.       {
  148.       tw->x2 -= tw->fxsize;
  149.       }
  150.  
  151. #ifdef OLD_DEBUG
  152.    sprintf( bw_ebuf, "right position (x2): %d [max_x2 is %d] ",
  153.       tw->x2, max_x2 );
  154.    bw_debug( bw_ebuf );
  155. #endif
  156.  
  157.    /* now set number of text columns based on this */
  158.  
  159.    tw->columns = ( tw->x2 - tw->x1 ) / tw->fxsize;
  160.  
  161.    /* calculate requested bottom screen position */
  162.  
  163.    tw->y1 = tw->y2 - ( rq_lines * tw->fysize );
  164.  
  165.    /* back up to an acceptable position */
  166.  
  167.    while ( tw->y1 < min_y1 )
  168.       {
  169.       tw->y1 += tw->fysize;
  170.       }
  171.  
  172.    /* now set number of text lines based on this */
  173.  
  174.    tw->lines = ( tw->y2 - tw->y1 ) / tw->fysize;
  175.  
  176. #ifdef OLD_DEBUG
  177.    sprintf( bw_ebuf, "Lines: %d    Columns %d",
  178.       tw->lines, tw->columns );
  179.    bw_debug( bw_ebuf );
  180.    sprintf( bw_ebuf, "x1 %d, y1 %d, x2 %d, y2 %d",
  181.       tw->x1, tw->y1, tw->x2, tw->y2 );
  182.    bw_debug( bw_ebuf );
  183.    sprintf( bw_ebuf, "set text window: y2 %d, x1 %d, y1 %d, x2 %d",
  184.            DEF_LINES - ( tw->y2 / tw->fysize ) + 1,
  185.            ( tw->x1 / tw->fxsize ) + 1,
  186.            ( DEF_LINES - ( tw->y1 / tw->fysize ) ),
  187.            ( ( tw->x2 / tw->fxsize ) - 1 ));
  188.    bw_debug( bw_ebuf );
  189. #endif
  190.  
  191.    /* set text window */
  192.  
  193.    _settextwindow(
  194.            DEF_LINES - ( tw->y2 / tw->fysize ) + 1,
  195.            ( tw->x1 / tw->fxsize ) + 1,
  196.            ( DEF_LINES - ( tw->y1 / tw->fysize ) ),
  197.            ( ( tw->x2 / tw->fxsize ) - 1 ));
  198.  
  199.    _wrapon( _GWRAPOFF );
  200.  
  201.    cur_twstruct = tw;
  202.  
  203.    return tw;
  204.  
  205.    }
  206.  
  207. tw_deinit( tw )
  208.    struct tw_struct *tw;
  209.    {
  210.    free( tw );
  211.    }
  212.  
  213. tw_outc( c )
  214.    int c;
  215.    {
  216.    char s[ 2 ];
  217.  
  218.    msm_hide();
  219.  
  220.    s[ 0 ] = c;
  221.    s[ 1 ] = 0;
  222.    _outtext( s );
  223.  
  224.    msm_show();
  225.  
  226.    }
  227.  
  228. tw_outs( s, line, column, color )
  229.    char *s;
  230.    int line, column, color;
  231.    {
  232.  
  233.    msm_hide();
  234.  
  235.    _settextposition( line + 1, column + 1 );
  236.    _settextcolor( ibm_color( color ) );
  237.    _outtext( s );
  238.  
  239.    msm_show();
  240.  
  241.    }
  242.  
  243. tw_adr( line, column )
  244.    int line, column;
  245.    {
  246.  
  247.    msm_hide();
  248.    _settextposition( line + 1, column + 1 );
  249.    msm_show();
  250.    }
  251.  
  252. tw_cursor( action )
  253.   int action;
  254.   {
  255.   if ( action == TRUE )
  256.      {
  257.      _settextcursor( (short) 0x0607 );
  258.      _displaycursor( _GCURSORON );
  259.      }
  260.   else
  261.      {
  262.      _settextcursor( (short) 0x2000 );
  263.      _displaycursor( _GCURSOROFF );
  264.      }
  265.   }
  266.  
  267. tw_cleol( line, column )
  268.    int line, column;
  269.    {
  270.    int x1, y2;
  271.  
  272.    x1 = cur_twstruct->x1 + ( column * cur_twstruct->fxsize );
  273.    y2 = cur_twstruct->y2 - ( line * cur_twstruct->fysize );
  274.    gr_rectangle( GR_PRIMARY, x1, y2 - ( cur_twstruct->fysize - 1 ),
  275.       cur_twstruct->x2, y2, BLACK, SOLID );
  276.  
  277.    }
  278.